const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 获取当前日期
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  // var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
  //   + " " + date.getHours() + seperator2 + date.getMinutes()
  //   + seperator2 + date.getSeconds();
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
}


// 获取当前日期时分秒
function getNowFormatDateHHMMSS() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
  // var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

//获取时间前后多少个小时的日期
function getTheDateBeforeOrAfter(dateString,hour,isBefore) {
  //获取到dateString的时间戳
  var timestamp = Date.parse(dateString);

  var new_timetamp;
  if (isBefore == true) {// the day before
    new_timetamp = timestamp - hour * 60 * 60 * 1000;
  }
  else {// the day after
    new_timetamp = timestamp + hour * 60 * 60 * 1000;
  }
  var newDate = new Date(new_timetamp);

  var seperator1 = "-";
  var seperator2 = ":";
  var month = newDate.getMonth() + 1;
  var strDate = newDate.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = newDate.getHours() + seperator2 + newDate.getMinutes();
  return currentdate;
}

// get the date before or after,
function getTheDayBeforeDate(dateString,isBefore) {
  //获取到dateString的时间戳
  var timestamp = Date.parse(dateString);

  var new_timetamp;
  if (isBefore == true) {// the day before
    new_timetamp = timestamp - 24 * 60 * 60 * 1000;
  }
  else {// the day after
    new_timetamp = timestamp + 24 * 60 * 60 * 1000;
  }
  var newDate = new Date(new_timetamp);

  var seperator1 = "-";
  var seperator2 = ":";
  var month = newDate.getMonth() + 1;
  var strDate = newDate.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = newDate.getFullYear() + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

//获取日期对应的是星期几
function getTheDateWeek(dateStr) {
  var timestamp = Date.parse(dateStr);
  var todayDate = new Date(timestamp);
  var dateweek = '';
  switch (todayDate.getDay()) {
    case 0: dateweek += "星期日"; break;
    case 1: dateweek += "星期一"; break;
    case 2: dateweek += "星期二"; break;
    case 3: dateweek += "星期三"; break;
    case 4: dateweek += "星期四"; break;
    case 5: dateweek += "星期五"; break;
    case 6: dateweek += "星期六"; break;
  }
  return dateweek;
}

//根据YYMMDD 取 MMDD
function getMMDD(dateStr) {
  var dateArr = dateStr.split('-');
  var newDate = dateArr[1] + '-' + dateArr[2];
  return newDate;
}

function getDate(dateStr,isNeedDate,hasT) {
  var dateArr = new Array();
  if(hasT) {
    dateArr = dateStr.split('T');
  }else {
    dateArr = dateStr.split(' ');
  }
  if(isNeedDate) {
    return dateArr[0].split('/').join('-');;
  }else {
    return dateArr[1];
  }

}

// 打印对象信息
function logObject(myObject) {
  var des = "";
  for (var i in myObject) {
    des += i + "=" + myObject[i] + "\n";
  }
  return des;
}

module.exports = {
  formatTime: formatTime,
  getNowFormatDate: getNowFormatDate,
  getNowFormatDateHHMMSS: getNowFormatDateHHMMSS,
  getTheDayBeforeDate: getTheDayBeforeDate,
  logObject: logObject,
  getTheDateWeek: getTheDateWeek,
  getMMDD: getMMDD,
  getDate: getDate,
  getTheDateBeforeOrAfter: getTheDateBeforeOrAfter,
}
